/*
 * Copyright 2009 Gal Dolber
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.dragdrop.client.impl;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.dragdrop.client.DragController;
import com.google.gwt.user.dragdrop.client.ResizeController;
import com.google.gwt.user.dragdrop.client.util.ResizeHandler;

/**
 * Standard resize implementation
 */
public class ResizeImpl extends ResizeHandler {
	public void BC(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth(), controller
				.getInitHeight()
				+ mouseController.getDeltaY());
	}

	public void BL(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth()
				- mouseController.getDeltaX(), controller.getInitHeight()
				+ mouseController.getDeltaY());
		controller.setPosition(controller
				.getFixedX(mouseController.getDeltaX()), controller.getInitY());
	}

	public void BR(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth()
				+ mouseController.getDeltaX(), controller.getInitHeight()
				+ mouseController.getDeltaY());

	}

	public void CL(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth()
				- mouseController.getDeltaX(), controller.getInitHeight());
		controller.setPosition(controller
				.getFixedX(mouseController.getDeltaX()), controller.getInitY());
	}

	public void CR(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth()
				+ mouseController.getDeltaX(), controller.getInitHeight());
	}

	@Override
	public void onResize(ResizeController controller,
			DragController mouseController) {

		// Only with LEFT button
		if (!mouseController.isMouseDown()
				|| mouseController.getButton() != NativeEvent.BUTTON_LEFT) {
			return;
		}

		switch (controller.getTargetBorder()) {
		case TOPLEFT:
			TL(controller, mouseController);
			break;
		case TOPCENTER:
			TC(controller, mouseController);
			break;
		case TOPRIGHT:
			TR(controller, mouseController);
			break;
		case CENTERLEFT:
			CL(controller, mouseController);
			break;
		case CENTERRIGHT:
			CR(controller, mouseController);
			break;
		case BOTTOMLEFT:
			BL(controller, mouseController);
			break;
		case BOTTOMCENTER:
			BC(controller, mouseController);
			break;
		case BOTTOMRIGHT:
			BR(controller, mouseController);
			break;
		case CENTERCENTER:
			break;
		case INVALID:
			break;
		}
	}

	public void TC(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth(), controller
				.getInitHeight()
				- mouseController.getDeltaY());
		controller.setPosition(controller.getInitX(), controller
				.getFixedY(mouseController.getDeltaY()));
	}

	public void TL(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth()
				- mouseController.getDeltaX(), controller.getInitHeight()
				- mouseController.getDeltaY());

		controller.setPosition(controller
				.getFixedX(mouseController.getDeltaX()), controller
				.getFixedY(mouseController.getDeltaY()));

	}

	public void TR(ResizeController controller, DragController mouseController) {
		controller.setSize(controller.getInitWidth()
				+ mouseController.getDeltaX(), controller.getInitHeight()
				- mouseController.getDeltaY());
		controller.setPosition(controller.getInitX(), controller
				.getFixedY(mouseController.getDeltaY()));
	}
}
